package one.jieyun.ajzw.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.Toast;

import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;

import java.util.List;

import one.jieyun.ajzw.Activity_View;
import one.jieyun.ajzw.R;
import one.jieyun.ajzw.module.ListViewItem;
import one.jieyun.ajzw.module.ListViewSubItem;

/**
 * Created by Administrator on 2016/8/31.
 */
public class HomeAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<ListViewItem> myList;

    @Override
    public int getCount() {
        return myList.size();
    }

    public HomeAdapter(Context context, List<ListViewItem> myList){
        this.myList = myList;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public Object getItem(int position) {
        return myList.get(position);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final Context context = arg2.getContext();
        ListViewItem item = myList.get(position);
        int type= item.getType();

        ViewHolder_Slider view_slider;
        ViewHolder_Opts view_opts;
        ViewHolder_News view_news;
        ViewHolder_HotProject view_hotproject;

        if(convertView == null) {
            switch (type) {
                case ListViewItem.ItemType_Slider:
                    convertView = mInflater.inflate(R.layout.item_sliders,null);
                    view_slider = new ViewHolder_Slider(convertView);
                    view_slider.rollSlider = (RollPagerView) convertView.findViewById(R.id.view_sliders);
                    view_slider.rollSlider.setPlayDelay(5000);
                    view_slider.rollSlider.setAnimationDurtion(500);
                    final List<ListViewSubItem> list = item.getItems();

                    view_slider.rollSlider.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(int position) {
                            ListViewSubItem slider = list.get(position);
                            Toast.makeText(context,slider.getImage(),Toast.LENGTH_SHORT).show();
                            openActivity(context,slider);
                        }
                    });

                    SliderAdapter mSliderAdapter = new SliderAdapter(view_slider.rollSlider);
                    view_slider.rollSlider.setAdapter(mSliderAdapter);
                    mSliderAdapter.setLists(list);

                    convertView.setTag(view_slider);
                    break;
                case ListViewItem.ItemType_Opts:
                    convertView = mInflater.inflate(R.layout.item_opts,null);
                    view_opts = new ViewHolder_Opts(convertView);
                    OptsAdapter optsAdapter = new OptsAdapter(context,item.getItems());

                    view_opts.gridView = (GridView)convertView.findViewById(R.id.view_opts);
                    view_opts.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            ListViewSubItem item = (ListViewSubItem)adapterView.getAdapter().getItem(i);
                            openActivity(context,item);
                        }
                    });

                    view_opts.gridView.setAdapter(optsAdapter);
                    convertView.setTag(view_opts);
                    break;
                case ListViewItem.ItemType_News:
                    convertView = mInflater.inflate(R.layout.item_news,null);
                    view_news = new ViewHolder_News(convertView);
                    view_news.rollNews = (RollPagerView)convertView.findViewById(R.id.view_news);
                    view_news.rollNews.setHintView(null);

                    final List<ListViewSubItem> list_news = item.getItems();

                    view_news.rollNews.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(int position) {
                            ListViewSubItem slider = list_news.get(position);
                            Toast.makeText(context,slider.getImage(),Toast.LENGTH_SHORT).show();
                            openActivity(context,slider);
                        }
                    });

                    NewsAdapter mNewsAdapter = new NewsAdapter(view_news.rollNews);
                    view_news.rollNews.setAdapter(mNewsAdapter);
                    mNewsAdapter.setLists(list_news);

                    convertView.setTag(view_news);
                    break;
                case ListViewItem.ItemType_HotProject:
                    convertView = mInflater.inflate(R.layout.item_opts,null);
                    view_hotproject = new ViewHolder_HotProject(convertView);
                    OptsAdapter hotAdapter = new OptsAdapter(context,item.getItems());

                    view_hotproject.gridView = (GridView)convertView.findViewById(R.id.view_opts);
                    view_hotproject.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            ListViewSubItem item = (ListViewSubItem)adapterView.getAdapter().getItem(i);
                            openActivity(context,item);
                        }
                    });

                    view_hotproject.gridView.setAdapter(hotAdapter);
                    convertView.setTag(view_hotproject);
                    break;
                default:
                    break;
            }

        }else{
            switch (type) {
                case ListViewItem.ItemType_Slider:
                    view_slider = (ViewHolder_Slider)convertView.getTag();
                    break;
                case ListViewItem.ItemType_Opts:
                    view_opts=(ViewHolder_Opts)convertView.getTag();
                    break;
                case ListViewItem.ItemType_News:
                    view_news = (ViewHolder_News)convertView.getTag();
                    break;
                case ListViewItem.ItemType_HotProject:
                    view_hotproject = (ViewHolder_HotProject)convertView.getTag();
                    break;
                default:
                    break;
            }
        }

        return convertView;
    }

    /**
     * 返回所有的layout的数量
     * */
    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getItemViewType(int position) {
        return myList.get(position).getType();
    }

    class ViewHolder_Slider{
        RollPagerView rollSlider;
        ViewHolder_Slider(View view){
            rollSlider = (RollPagerView)view.findViewById(R.id.view_sliders);
        }
    }

    class ViewHolder_Opts{
        GridView gridView;
        ViewHolder_Opts(View view){
            gridView = (GridView)view.findViewById(R.id.view_opts);
        }
    }

    class ViewHolder_News{
        RollPagerView rollNews;
        ViewHolder_News(View view){
            rollNews = (RollPagerView)view.findViewById(R.id.view_news);
        }
    }

    class ViewHolder_HotProject{
        GridView gridView;
        ViewHolder_HotProject(View view){
            gridView = (GridView)view.findViewById(R.id.view_project);
        }
    }

    private void openActivity(Context context,ListViewSubItem item){
        Intent intent = new Intent(context,Activity_View.class);
        Bundle bundle = new Bundle();
        bundle.putString("url",item.getUrl());
        bundle.putString("title",item.getTitle());
        intent.putExtras(bundle);
        context.startActivities(new Intent[]{intent});
    }

}
